package com.ruoyi.backend.controller;

import com.ruoyi.backend.domain.Member;
import com.ruoyi.backend.service.IMemberService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户列表Controller
 *
 * @author 王冰冰
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/userList")
public class MemberController extends BaseController {
    @Autowired
    private IMemberService memberService;

    /**
     * 查询用户列表列表
     */
    @RequiresPermissions("userManagement:userList:list")
    @GetMapping("/list")
    public TableDataInfo list(Member member) {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出用户列表列表
     */
    @RequiresPermissions("userManagement:userList:export")
    @Log(title = "用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Member member) {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        util.exportExcel(response, list, "用户列表数据");
    }

    /**
     * 获取用户列表详细信息
     */
    @RequiresPermissions("userManagement:userList:query")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId) {
        return success(memberService.selectMemberByMemberId(memberId));
    }

    /**
     * 新增用户列表
     */
    @RequiresPermissions("userManagement:userList:add")
    @Log(title = "用户列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member) {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改用户列表
     */
    @RequiresPermissions("userManagement:userList:edit")
    @Log(title = "用户列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member member) {
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除用户列表
     */
    @RequiresPermissions("userManagement:userList:remove")
    @Log(title = "用户列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds) {
        return toAjax(memberService.deleteMemberByMemberIds(memberIds));
    }
}